package me.winds.wrapper.base.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;

import me.winds.wrapper.R;
import me.winds.wrapper.base.adapter.ViewHelper;
import me.winds.wrapper.base.presenter.BaseMvpPresenter;
import me.winds.wrapper.base.presenter.MvpPresenter;
import me.winds.wrapper.core.NetworkChanged;
import me.winds.wrapper.core.network.ResponseInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.lang.reflect.ParameterizedType;

/**
 * 封装了网络层的Fragment
 *
 * @param <P>
 */
public abstract class WrapperMvpFragment<P extends BaseMvpPresenter> extends WrapperFragment implements MvpView {
    protected P presenter;
    protected WrapperDialog progressDialog;
    private boolean loadSuccess;   //请求是否成功
    private boolean loadFinish; //请求是否结束

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresenter(this.getClass());
        presenter.attachView(this);
        progressDialog = createLoadingDialog(getActivity());
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onStopLoading();
        progressDialog = null;
        presenter.detachView();
        presenter = null;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void beforeSuccess() {
        loadSuccess = true;
        loadFinish = true;
    }

    @Override
    public void onLoading() {
        showLoadingDialog();
        loadSuccess = false;
        loadFinish = false;
    }

    @Override
    public void onStopLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onLoadError(ResponseInfo responseInfo) {
        loadFinish = true;
        loadSuccess = false;
    }

    @Subscribe
    public void networkChanged(NetworkChanged changed) {

    }

    protected void showLoadingDialog() {
        if (progressDialog != null && !progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    protected P createPresenter(Class clz) {
        ParameterizedType type = (ParameterizedType) clz.getGenericSuperclass();
        Class<? extends MvpPresenter> presenterClazz = (Class<? extends MvpPresenter>) type.getActualTypeArguments()[0];
        try {
            return (P) presenterClazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected WrapperDialog createLoadingDialog(Context context) {
        return new WrapperDialog(context) {
            @Override
            public int getLayoutRes() {
                return R.layout.layout_progress_bar;
            }

            @Override
            protected void setDialogParams(Dialog dialog) {
                setDialogParams(dialog, Gravity.CENTER);
                dialog.setCanceledOnTouchOutside(false);
            }

            @Override
            public void help(ViewHelper helper) {

            }
        };
    }

    private void showOpenNetworkDialog(ResponseInfo responseInfo) {
        showToast("网络连接不可用，请打开网络！");
    }

    protected void showTimeOutDialog(ResponseInfo responseInfo) {
        showToast("连接网络超时");
    }

}
